package com.utils.protobuffer;

import com.google.protobuf.InvalidProtocolBufferException;
import com.utils.protobuffer.personProto.Person;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;

public class WriteMessage {
  // This function fills in a Person message based on user input.
  static Person PromptForAddress(BufferedReader stdin,
                                 PrintStream stdout) throws IOException {
    Person.Builder person = Person.newBuilder();

    stdout.print("Enter person ID: ");
    person.setId(Integer.valueOf(stdin.readLine()));

    stdout.print("Enter name: ");
    person.setName(stdin.readLine());

    stdout.print("Enter email address (blank for none): ");
    String email = stdin.readLine();
    if (email.length() > 0) {
      person.setEmail(email);
    }

//    while (true) {
//      stdout.print("Enter a phone number (or leave blank to finish): ");
//      String number = stdin.readLine();
//      if (number.length() == 0) {
//        break;
//      }
//
//      Person.PhoneNumber.Builder phoneNumber =
//        Person.PhoneNumber.newBuilder().setNumber(number);

//      stdout.print("Is this a mobile, home, or work phone? ");
//      String type = stdin.readLine();
//      if (type.equals("mobile")) {
//        phoneNumber.setType(Person.PhoneType.MOBILE);
//      } else if (type.equals("home")) {
//        phoneNumber.setType(Person.PhoneType.HOME);
//      } else if (type.equals("work")) {
//        phoneNumber.setType(Person.PhoneType.WORK);
//      } else {
//        stdout.println("Unknown phone type.  Using default.");
//      }
//
//      person.addPhones(phoneNumber);
//    }

    return person.build();
  }

  // Main function:  Reads the entire address book from a file,
  //   adds one person based on user input, then writes it back out to the same
  //   file.
 /* public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("Usage:  AddPerson ADDRESS_BOOK_FILE");
      System.exit(-1);
    }

    AddressBook.Builder addressBook = AddressBook.newBuilder();

    // Read the existing address book.
    try {
      addressBook.mergeFrom(new FileInputStream(args[0]));
    } catch (FileNotFoundException e) {
      System.out.println(args[0] + ": File not found.  Creating a new file.");
    }

    // Add an address.
    addressBook.addPeople(
      PromptForAddress(new BufferedReader(new InputStreamReader(System.in)),
                       System.out));

    // Write the new address book back to disk.
    FileOutputStream output = new FileOutputStream(args[0]);
    addressBook.build().writeTo(output);
    output.close();
  }*/
  
  public static void main(String[] args) {

	// 创建builder对象
	personProto.Person.Builder builder = personProto.Person.newBuilder();
	builder.setEmail("ggggggg@email.com");
	builder.setId(1);
	builder.setName("TestName");
//	builder.addPhone(personProto.Person.PhoneNumber.newBuilder().setNumber("13664262866")
//	.setType(personProto.Person.PhoneType.MOBILE));
//	builder.addPhone(personProto.Person.PhoneNumber.newBuilder().setNumber("02869387891")
//	.setType(personProto.Person.PhoneType.HOME));
	// 通过builder转化为Person对象
	Person person = builder.build();
	// 将person对象转化为字节流
	byte[] buf = person.toByteArray();
	 
	try {
	// 通过字节流转化 为Person对象
	Person person2 = personProto.Person.parseFrom(buf);
	System.out.println(person2.getId()+","+person2.getName() + ", " + person2.getEmail());
//	List<PhoneNumber> lstPhones = person2.getPhoneList();
//	for (PhoneNumber phoneNumber : lstPhones) {
//	System.out.println(phoneNumber.getNumber());
//	}
	} catch (InvalidProtocolBufferException e) {
			e.printStackTrace();
	}
	 
	System.out.println(buf.toString());
  }
}